//
//  LZCreatePromotionTableView.m
//  laziz_Merchant
//
//  Created by ZhaoDongBo on 2016/12/17.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZCreatePromotionTableView.h"
#import "LZCreatePromoitonHeaderView.h"
#import "LZTextViewTableViewCell.h"
#import "LZImageTableViewCell.h"
#import "LZLabelTableViewCell.h"
#import "LZSelectTableViewCell.h"
#import "LZDescriptionTableViewCell.h"

#import "LZMyRestaurantInfoTimeTableViewCell.h"
#import "LZMyRestaurantInfoTextFieldTableViewCell.h"

#import "Masonry.h"

#import "LZCouponListModel.h"

@interface LZCreatePromotionTableView() <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) LZCreatePromoitonHeaderView * headerView1;
@property (nonatomic, strong) LZCreatePromoitonHeaderView * headerView2;

@end

@implementation LZCreatePromotionTableView

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    self = [super initWithFrame:frame style:style];
    if (self) {
        [self setup];
    }
    return self;
}

#pragma
#pragma mark private method

- (void)setup {
    self.backgroundColor = UIColorFromRGB(0xf9f7f7,1);
    self.separatorColor = [UIColor clearColor];
    self.dataSource = self;
    self.delegate = self;
}

#pragma
#pragma mark UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0) {
        return 4;
    }
    else if (section == 1 && self.isShowCoupon) {
        return 9;
    }
    else if (section == 2 && self.isShowGroup) {
        return 11;
    }
    
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    WS(weakSelf);
    
    if (indexPath.section == 1) {
        
        if (indexPath.row == 1) { //Coupon amount
            LZMyRestaurantInfoTextFieldTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"textField3CellIdentifier"];
            if (!cell) {
                cell = [[LZMyRestaurantInfoTextFieldTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                                                       reuseIdentifier:@"textField3CellIdentifier"];
            }
            
            [cell setTextfieldTag:100+indexPath.row];
            [cell setCouponListModel:weakSelf.couponListModel];
            cell.indexPath = indexPath;
            cell.textFieldEditDidEndBlock = ^(NSString * text, NSInteger tag,NSIndexPath *indexP) {
                if (weakSelf.textValueEditDidEndBlock) {
                    weakSelf.textValueEditDidEndBlock(text, tag, indexP);
                }
            };
            
            return cell;
        }
//        else if (indexPath.row == 2) { //优惠平台选择
//            LZSelectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"selectCellIdentifier"];
//            if (!cell) {
//                cell = [[LZSelectTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
//                                                    reuseIdentifier:@"selectCellIdentifier"];
//            }
//            
//            [cell setCouponListModel:self.couponListModel andTag:indexPath.row];
//            cell.selectButtonActionBlock = ^(NSInteger value) {
//                if (weakSelf.selectButtonActionBlock) {
//                    weakSelf.selectButtonActionBlock(indexPath, value);
//                }
//            };
//            
//            return cell;
//        }
        else if (indexPath.row == 4 || indexPath.row == 6) { //5:Valid from/to   7:Everyday valid from/to
            LZMyRestaurantInfoTimeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"timeCellIdentifier"];
            if (!cell) {
                cell = [[LZMyRestaurantInfoTimeTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                                                  reuseIdentifier:@"timeCellIdentifier"];
            }
        
            cell.tag = indexPath.row;
            [cell setCouponListModel:self.couponListModel];
            cell.tapGRActionBlock = ^(id sender){
                if (weakSelf.tapGRActionBlock) {
                    weakSelf.tapGRActionBlock(indexPath, sender);
                }
            };
            
            return cell;
        }
        else {
            LZTextViewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"textViewCellIdentifier"];
            if (!cell) {
                cell = [[LZTextViewTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                                      reuseIdentifier:@"textViewCellIdentifier"];
            }
            
            [cell setTextViewTag:100+indexPath.row];
            [cell setCouponListModel:weakSelf.couponListModel];
            cell.indexPath = indexPath;
            cell.textViewEditDidEndBlock = ^(NSString * text, NSInteger tag, NSIndexPath *indexP) {
                if (weakSelf.textValueEditDidEndBlock) {
                    weakSelf.textValueEditDidEndBlock(text, tag, indexP);
                }
            };

            return cell;
        }
    }
    else if (indexPath.section == 2) {
        if (indexPath.row == 1 || indexPath.row == 2 || indexPath.row == 5 || indexPath.row == 6) {
            LZLabelTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"labelCellIdentifier"];
            if (!cell) {
                cell = [[LZLabelTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                                   reuseIdentifier:@"labelCellIdentifier"];
            }
            
            cell.tag = indexPath.row;
            [cell setCouponListModel:self.couponListModel];
            
            return cell;
        }
        else if (indexPath.row == 3) {// 团购价
            LZMyRestaurantInfoTextFieldTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"textField2CellIdentifier"];
            if (!cell) {
                cell = [[LZMyRestaurantInfoTextFieldTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                                                       reuseIdentifier:@"textField2CellIdentifier"];
            }
            
            [cell setTextfieldTag:200+indexPath.row];
            [cell setCouponListModel:self.couponListModel];
            cell.indexPath = indexPath;
            cell.textFieldEditDidEndBlock = ^(NSString * text, NSInteger tag, NSIndexPath *indexP) {
                if (weakSelf.textValueEditDidEndBlock) {
                    weakSelf.textValueEditDidEndBlock(text, tag, indexP);
                }
            };
            
            return cell;
        }
//        else if (indexPath.row == 4) { //优惠平台选择
//            LZSelectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"selectGroupCellIdentifier"];
//            if (!cell) {
//                cell = [[LZSelectTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
//                                                                   reuseIdentifier:@"selectGroupCellIdentifier"];
//            }
//            
//            [cell setCouponListModel:self.couponListModel andTag:indexPath.row];
//            cell.selectButtonActionBlock = ^(NSInteger value) {
//                if (weakSelf.selectButtonActionBlock) {
//                    weakSelf.selectButtonActionBlock(indexPath, value);
//                }
//            };
//            
//            return cell;
//        }
        else if (indexPath.row == 8) {  //group everyday valid from/to
            LZMyRestaurantInfoTimeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"timeCellIdentifier"];
            if (!cell) {
                cell = [[LZMyRestaurantInfoTimeTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                                                  reuseIdentifier:@"timeCellIdentifier"];
            }
            
            cell.tag = indexPath.row;
            [cell setCouponListModel:self.couponListModel];
            cell.tapGRActionBlock = ^(id sender){
                if (weakSelf.tapGRActionBlock) {
                    weakSelf.tapGRActionBlock(indexPath, sender);
                }
            };
            
            return cell;
        }
        else {
            LZTextViewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"textViewCellIdentifier"];
            if (!cell) {
                cell = [[LZTextViewTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                                      reuseIdentifier:@"textViewCellIdentifier"];
            }
            
            [cell setTextViewTag:200+indexPath.row];
            [cell setCouponListModel:weakSelf.couponListModel];
            cell.indexPath = indexPath;
            cell.textViewEditDidEndBlock = ^(NSString * text, NSInteger tag, NSIndexPath *indexP) {
                if (weakSelf.textValueEditDidEndBlock) {
                    weakSelf.textValueEditDidEndBlock(text, tag, indexP);
                }
            };
            
            return cell;
        }
    }
    else {
        
        if (indexPath.row == 0) {
            LZImageTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"imageCellIdentifier"];
            if (!cell) {
                cell = [[LZImageTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                                   reuseIdentifier:@"imageCellIdentifier"];
            }
            
            [cell setTextFieldTag:300+indexPath.row];
            [cell setCouponListModel:self.couponListModel];
            cell.indexPath = indexPath;
            cell.textFieldEditDidEndBlock = ^(NSString * text, NSInteger tag, NSIndexPath *indexP) {
                if (weakSelf.textValueEditDidEndBlock) {
                    weakSelf.textValueEditDidEndBlock(text, tag,indexP);
                }
            };
            
            return cell;
        }
        else if (indexPath.row == 3) {
            LZDescriptionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"descriptionCellIdentifier"];
            if (!cell) {
                cell = [[LZDescriptionTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                                                        reuseIdentifier:@"descriptionCellIdentifier"];
            }

            [cell setTextViewTag:300+indexPath.row];
            [cell setCouponListModel:weakSelf.couponListModel];
            cell.indexPath = indexPath;
            cell.textViewEditDidEndBlock = ^(NSString * text, NSInteger tag, NSIndexPath *indexP) {
                if (weakSelf.textValueEditDidEndBlock) {
                    weakSelf.textValueEditDidEndBlock(text, tag, indexP);
                }
            };
            
            return cell;
        }
        else {
            LZTextViewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"textViewCellIdentifier"];
            if (!cell) {
                cell = [[LZTextViewTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                                      reuseIdentifier:@"textViewCellIdentifier"];
            }

            [cell setTextViewTag:300+indexPath.row];
            [cell setCouponListModel:weakSelf.couponListModel];
            cell.indexPath = indexPath;
            cell.textViewEditDidEndBlock = ^(NSString * text, NSInteger tag, NSIndexPath *indexP) {
                if (weakSelf.textValueEditDidEndBlock) {
                    weakSelf.textValueEditDidEndBlock(text, tag, indexP);
                }
            };
            
            return cell;
        }
    }
}

#pragma
#pragma mark UITableViewDelegate

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    if (section == 1) {
        return self.headerView1;
    }
    else if (section == 2){
        return self.headerView2;
    }
    
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 1 || section == 2) {
        return 55.0f;
    }
    
    return 0.001f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 0 || section == 1) {
        return 10.0f;
    }
    
    return 0.001f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            return 95;
        }
        else if (indexPath.row == 1) {
            if (self.couponListModel.promotionFood.length > 0) {
                return [LZTextViewTableViewCell getCellHeightWithText:self.couponListModel.promotionFood];
            } else {
                return [LZTextViewTableViewCell getCellHeightWithText:LZLocalizedString(@"please_enter_ingredients_components", nil)];
            }
        }
        else if (indexPath.row == 2) {
            if (self.couponListModel.tag.length > 0) {
                return [LZTextViewTableViewCell getCellHeightWithText:self.couponListModel.tag];
            } else {
                return [LZTextViewTableViewCell getCellHeightWithText:LZLocalizedString(@"please_enter_tags", nil)];
            }
        }
        else {
            return 200.0f;
        }
    }
    else if (indexPath.section == 1) {

        if (indexPath.row == 0) {
            if (self.couponListModel.couponsName.length > 0) {
                return [LZTextViewTableViewCell getCellHeightWithText:self.couponListModel.couponsName];
            } else {
                return [LZTextViewTableViewCell getCellHeightWithText:LZLocalizedString(@"please_enter_coupon_name", nil)];
            }
        }
        else if (indexPath.row == 2) {
            if (self.couponListModel.couponsUseConditions.length > 0) {
                return [LZTextViewTableViewCell getCellHeightWithText:self.couponListModel.couponsUseConditions];
            } else {
                return [LZTextViewTableViewCell getCellHeightWithText:LZLocalizedString(@"coupon_valid_conditions", nil)];
            }
        }
        else if (indexPath.row == 3) {
            if (self.couponListModel.couponsTotalCount > 0) {
                return [LZTextViewTableViewCell getCellHeightWithText:[NSString stringWithFormat:@"%ld", (long)self.couponListModel.couponsTotalCount]];
            } else {
                return [LZTextViewTableViewCell getCellHeightWithText:LZLocalizedString(@"please_enter_coupons_to_issue", nil)];
            }
        }
        else if (indexPath.row == 5) {
            if (self.couponListModel.couponsExceptTime.length > 0) {
                return [LZTextViewTableViewCell getCellHeightWithText:self.couponListModel.couponsExceptTime];
            } else {
                return [LZTextViewTableViewCell getCellHeightWithText:LZLocalizedString(@"please_select_the_date", nil)];
            }
        }
        else if (indexPath.row == 7) {
            if (self.couponListModel.couponsRemindAppoint.length > 0) {
                return [LZTextViewTableViewCell getCellHeightWithText:self.couponListModel.couponsRemindAppoint];
            } else {
                return [LZTextViewTableViewCell getCellHeightWithText:LZLocalizedString(@"please_need_to_book", nil)];
            }
        }
        else if (indexPath.row == 8) {
            if (self.couponListModel.couponsRemindRule.length > 0) {
                return [LZTextViewTableViewCell getCellHeightWithText:self.couponListModel.couponsRemindRule];
            } else {
                return [LZTextViewTableViewCell getCellHeightWithText:LZLocalizedString(@"please_other_conditions", nil)];
            }
        }
        else {
            return 55.0f;
        }
    }
    else if (indexPath.section == 2) {

        if (indexPath.row == 0) {
            if (self.couponListModel.grouponName.length > 0) {
                return [LZTextViewTableViewCell getCellHeightWithText:self.couponListModel.grouponName];
            } else {
                return [LZTextViewTableViewCell getCellHeightWithText:LZLocalizedString(@"please_enter_group_name", nil)];
            }
        }
        else if (indexPath.row == 4) {
            if (self.couponListModel.grouponCount > 0) {
                return [LZTextViewTableViewCell getCellHeightWithText:[NSString stringWithFormat:@"%ld", (long)self.couponListModel.grouponCount]];
            } else {
                return [LZTextViewTableViewCell getCellHeightWithText:LZLocalizedString(@"please_enter_group_number_people", nil)];
            }
        }
        else if (indexPath.row == 7) {
            if (self.couponListModel.grouponExceptTime.length > 0) {
                return [LZTextViewTableViewCell getCellHeightWithText:self.couponListModel.grouponExceptTime];
            } else {
                return [LZTextViewTableViewCell getCellHeightWithText:LZLocalizedString(@"please_select_the_date", nil)];
            }
        }
        else if (indexPath.row == 9) {
            if (self.couponListModel.grouponRemindAppoint.length > 0) {
                return [LZTextViewTableViewCell getCellHeightWithText:self.couponListModel.grouponRemindAppoint];
            } else {
                return [LZTextViewTableViewCell getCellHeightWithText:LZLocalizedString(@"please_need_to_book", nil)];
            }
        }
        else if (indexPath.row == 10) {
            if (self.couponListModel.grouponRemindRule.length > 0) {
                return [LZTextViewTableViewCell getCellHeightWithText:self.couponListModel.grouponRemindRule];
            } else {
                return [LZTextViewTableViewCell getCellHeightWithText:LZLocalizedString(@"please_other_conditions", nil)];
            }
        }
        else {
            return 55.0f;
        }
    }
    
    return 0;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 1 && indexPath.row == 1) {//针对 coupon 中的 coupon amount选项, 点击cell时吊起textfield
        LZMyRestaurantInfoTextFieldTableViewCell *cell = (LZMyRestaurantInfoTextFieldTableViewCell *)[self cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:1]];
        [cell callTextFieldBecomeFirstResponder];
    }else if (indexPath.section == 2 && indexPath.row == 3){
        LZMyRestaurantInfoTextFieldTableViewCell *cell = (LZMyRestaurantInfoTextFieldTableViewCell *)[self cellForRowAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:2]];
        [cell callTextFieldBecomeFirstResponder];
    }
    
    if(_didSelectRowBlock) {
        _didSelectRowBlock(indexPath);
    }
}

- (LZCreatePromoitonHeaderView *)headerView1 {
    
    if (_headerView1 == nil) {
        _headerView1 = [[LZCreatePromoitonHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 55.0f)];
        _headerView1.titleLabel.text = LZLocalizedString(@"copon", @"");
        
        if (self.isShowCoupon) {
            [_headerView1.selectButton setImage:[UIImage imageNamed:@"lz_select_check"] forState:UIControlStateNormal];
            _headerView1.indicatorImage.image = [UIImage imageNamed:@"lz_hide"];
        }
        else {
            [_headerView1.selectButton setImage:[UIImage imageNamed:@"lz_select_no"] forState:UIControlStateNormal];
            _headerView1.indicatorImage.image = [UIImage imageNamed:@"lz_show"];
        }
        
        WS(weakSelf);
        _headerView1.selectButtonActionBlock = ^(id sender) {
            
            [weakSelf endEditing:YES];
            
            if (weakSelf.isShowCoupon == NO) {
                [weakSelf.headerView1.selectButton setImage:[UIImage imageNamed:@"lz_select_check"] forState:UIControlStateNormal];
                weakSelf.headerView1.indicatorImage.image = [UIImage imageNamed:@"lz_hide"];
                weakSelf.isShowCoupon = YES;
                [weakSelf reloadData];
            } else {
                [weakSelf.headerView1.selectButton setImage:[UIImage imageNamed:@"lz_select_no"] forState:UIControlStateNormal];
                weakSelf.headerView1.indicatorImage.image = [UIImage imageNamed:@"lz_show"];
                weakSelf.isShowCoupon = NO;
                [weakSelf reloadData];
            }
        };
    }
    
    return _headerView1;
}

- (LZCreatePromoitonHeaderView *)headerView2 {
    
    if (_headerView2 == nil) {
        _headerView2 = [[LZCreatePromoitonHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 55.0f)];
        _headerView2.titleLabel.text = LZLocalizedString(@"group_order_title", @"");
        if (self.isShowGroup) {
            [_headerView2.selectButton setImage:[UIImage imageNamed:@"lz_select_check"] forState:UIControlStateNormal];
            _headerView2.indicatorImage.image = [UIImage imageNamed:@"lz_hide"];
        }
        else {
            [_headerView2.selectButton setImage:[UIImage imageNamed:@"lz_select_no"] forState:UIControlStateNormal];
            _headerView2.indicatorImage.image = [UIImage imageNamed:@"lz_show"];
        }
        
        WS(weakSelf);
        _headerView2.selectButtonActionBlock = ^(id sender) {
            
            [weakSelf endEditing:YES];
            
            if (weakSelf.isShowGroup == NO) {
                [weakSelf.headerView2.selectButton setImage:[UIImage imageNamed:@"lz_select_check"] forState:UIControlStateNormal];
                weakSelf.headerView2.indicatorImage.image = [UIImage imageNamed:@"lz_hide"];
                weakSelf.isShowGroup = YES;
                [weakSelf reloadData];
            } else {
                [weakSelf.headerView2.selectButton setImage:[UIImage imageNamed:@"lz_select_no"] forState:UIControlStateNormal];
                weakSelf.headerView2.indicatorImage.image = [UIImage imageNamed:@"lz_show"];
                weakSelf.isShowGroup = NO;
                [weakSelf reloadData];
            }
        };
    }
    
    return _headerView2;
}

@end
